/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2018-2020.
 * Description:
 *	 1. This header file is used for sysctl-related public function macros.
 *	 2. few sysctl variable declarations for small features without code files
 *
 * Note:
 *     If the feature has its own code file,  please use register_sysctl
 *     to register the sysctl interface in its own code file.
 *
 * Author: nixiaoming
 * Create: 2018-3-4
 */

#ifndef RTOS_KERNEL_SYSCTL_H
#define RTOS_KERNEL_SYSCTL_H

#include <linux/sysctl.h>
#include <linux/kmemleak.h>
#include <linux/module.h>
#include <linux/proc_fs.h>

/*
 * The sysctl interface is used to modify the interface value,
 * but the feature interface has default values. Even if register_sysctl fails,
 * the feature body function can also run. At the same time, malloc small
 * fragment of memory during the system initialization phase, almost does
 * not fail. Therefore, the function return is designed as void
 */
#define rtos_sysctls_proc_init(feature, sysctl, dir, dname) \
	static int __init rtos_##feature##_##dname##_sysctls_proc_init(void) \
	{ \
		struct ctl_table_header *p = register_sysctl(dir, sysctl); \
		if (p) \
			kmemleak_not_leak(p); \
		return 0; \
	} \
	module_init(rtos_##feature##_##dname##_sysctls_proc_init)

#define rtos_kernel_sysctls_proc_init(feature, sysctl) \
	rtos_sysctls_proc_init(feature, sysctl, "kernel", kernel)

#define rtos_debug_sysctls_proc_init(feature, sysctl) \
	rtos_sysctls_proc_init(feature, sysctl, "debug", debug)

#define rtos_net_sysctls_proc_init(feature, sysctl) \
	rtos_sysctls_proc_init(feature, sysctl, "net", net)

#define rtos_fs_sysctls_proc_init(feature, sysctl) \
	rtos_sysctls_proc_init(feature, sysctl, "fs", fs)

/*
 * extern  the sysctl variable here
 * Only for small features that do not have their own c-code file
 *
 * If the feature has its own code file,  please use register_sysctl
 * to register the sysctl interface in its own code file.
 */

#ifdef CONFIG_RTOS_NET_SUPPORT_DISABLE_ZERO_ADDRESS
extern int net_ipv4_zero_bt;
#endif

#ifdef CONFIG_RTOS_STRICT_DEVMEM_IOMEM_CONTROL_DEBUG
extern int sysctl_devmem_log;

static inline void devmem_strict_ctrl_logging(unsigned long long p)
{
	if (sysctl_devmem_log)
		pr_warn("[%d][%s]Iomem-phyaddr[0x%llx] is not allowed to read\n",
					current->pid, current->comm, p & PAGE_MASK);
}
#else
static inline void devmem_strict_ctrl_logging(unsigned long long p) {}
#endif

#ifdef CONFIG_RTOS_DEBUG_DUMP_CORRUPTED_STACK
extern unsigned long sysctl_stack_dump_size;
#endif

#ifdef CONFIG_RTOS_DEBUG_DUMP_USER_PROCESS
#ifdef CONFIG_RTOS_KBOX
extern unsigned int dump_user_process_log_size;
#endif
#endif

#endif
